<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input
        v-model="listQuery.title"
        clearable
        class="filter-item"
        style="width: 200px;"
        placeholder="商品名称/条码"
      />
      <el-select v-model="listQuery.warehouseId" clearable style="width: 200px" class="filter-item" placeholder="请选择仓库" >
        <el-option v-for="(item,index) in warehouseList" :key="index" :label="item.warehouseName" :value="item.id" />
      </el-select>
      <el-button
        v-permission="['warehouse:warehouseoperlog:query']"
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >搜索</el-button>
    </div>
    <!-- 更新对话框 -->
    <el-dialog
      v-loading="updateFormLoading"
      :visible.sync="updateDialogVisible"
      title="更新商品库存预警"
      width="35%">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="dataForm"
        status-icon
        label-position="left"
        label-width="100px"
      >
        <el-form-item v-if="hidden" prop="id"/>
        <el-form-item prop="title" label="商品名称">
          <el-col>{{ dataForm.title }}</el-col>
        </el-form-item>
        <el-form-item prop="warehouseName" label="设置仓库">
          <el-col>{{ dataForm.warehouseName }}</el-col>
        </el-form-item>
        <el-form-item prop="warehouseSkuStock" label="当前库存">
          <el-col>{{ dataForm.warehouseSkuStock }} &nbsp;&nbsp; ({{ dataForm.unit }})</el-col>
        </el-form-item>
        <el-form-item prop="alarm" label="预警阈值">
          <el-input v-model="dataForm.alarm" clearable placeholder="设置预警阈值" style="width:200px;">
            <template slot="append"><el-col>{{ dataForm.unit }}</el-col></template>
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="updateDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="updateGoodsAlarm('dataForm')">确认</el-button>
      </div>
    </el-dialog>
    <!-- 查询结果 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      size="small"
      element-loading-text="正在查询中。。。"
      border
      fit
      highlight-current-row
      style="white-space: pre-line"
    >
      <el-table-column align="center" label="商品名称">
        <template slot-scope="scope">
          <el-col>{{ scope.row.title }}</el-col>
        </template>
      </el-table-column>
      <el-table-column prop="warehouseName" align="center" label="仓库名称"/>
      <el-table-column prop="warehouseSkuStock" align="center" label="当前库存"/>
      <el-table-column align="center" label="库存阈值">
        <template slot-scope="scope">
          <span>{{ scope.row.alarm === 0 ? '未设置': scope.row.alarm }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="unit" align="center" label="单位"/>
      <el-table-column align="center" label="操作" width="300" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button
            v-permission="['warehouseAllocate:warehouseallocate:update']"
            v-if="scope.row.allocateState!='1'"
            type="primary"
            size="mini"
            @click="handleUpdate(scope.row)"
          >编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.pageNo"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
  </div>
</template>

<style>
  .el-table .cell {
    white-space: pre-line;
  }
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #20a0ff;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 120px;
    height: 120px;
    line-height: 120px;
    text-align: center;
  }
  .avatar {
    width: 100px;
    height: 120px;
    display: block;
  }
</style>

<script>
import { listGoodsAlarm, updateGoodsAlarm, detail } from '@/api/warehousegoods'
import { listWareHousesByPara } from '@/api/warehouse'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
const typeMap = {
  1: '入库',
  2: '出库',
  3: '盘点',
  4: '损耗',
  5: '调拨',
  6: '减少库存',
  7: '恢复库存',
  8: '退货',
  9: '退库'
}
export default {
  name: 'GoodsWarehouseAlarmList',
  components: { Pagination },
  filters: {
    typeFilter(status) {
      return typeMap[status]
    }
  },
  data() {
    return {
      list: undefined,
      total: 0,
      warehouseList: [],
      listLoading: true,
      listQuery: {
        pageNo: 1,
        limit: 20,
        timeInfo: ''
      },
      options: [],
      dataForm: {
        id: undefined,
        title: undefined,
        warehouseName: undefined,
        alarm: 0
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '创建'
      },
      rules: {
        alarm: [{ pattern: /^[+]?([0-9]+(.[0-9]{1,2})?)$/, message: '请输入整数或一到两位小数', trigger: 'blur' }]
      },
      updateFormLoading: false,
      updateDialogVisible: false
    }
  },
  created() {
    this.listWareHousesByPara()
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      listGoodsAlarm(this.listQuery)
        .then(response => {
          this.list = response.data.data.items
          this.total = response.data.data.total
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.pageNo = 1
      this.listQuery.timeInfo = this.listQuery.timeInfo && this.listQuery.timeInfo !== '' ? this.listQuery.timeInfo.join(',') : ''
      this.getList()
    },
    resetForm() {
      this.dataForm = {
        id: undefined
      }
    },
    listWareHousesByPara() {
      this.listQuery.status = 1
      listWareHousesByPara(this.listQuery)
        .then(response => {
          this.warehouseList = response.data.data
        })
        .catch(() => {
          this.warehouseList = []
        })
    },
    handleUpdate(row) {
      this.updateFormLoading = false
      this.detail(row.id)
      this.updateDialogVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateGoodsAlarm(dataFormName) {
      this.$refs[dataFormName].validate(valid => {
        if (valid) {
          updateGoodsAlarm(this.dataForm)
            .then(response => {
              this.updateFormLoading = false
              this.getList()
              this.dialogFormVisible = false
              this.$notify.success({
                title: '成功',
                message: '更新成功'
              })
              this.updateDialogVisible = false
            })
            .catch(response => {
              this.updateFormLoading = false
              this.$notify.error({
                title: '失败',
                message: '更新失败'
              })
            })
          this.updateDialogVisible = false
        }
      })
    },
    detail(id) {
      detail({ id: id })
        .then(response => {
          this.dataForm = response.data.data
        })
        .catch(response => {
          this.dataForm = {}
        })
    }
  }
}
</script>

